Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

String Templates

String templates allow you to include variable references and expressions into strings. When the value of a string is requested (for example, by println), all references and expressions are substituted with actual values.

fun main() {
    val greeting = "Kotliner"
    
    println("Hello $greeting")                  // 1 
    println("Hello ${greeting.uppercase()}")    // 2
}
  1. Prints a string with a variable reference. References in strings start with $.
  2. Prints a string with an expression. Expressions start with $ and are enclosed in curly braces.